﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp3
{
    public partial class FrmMain : Form
    {
        //游戏对象
        private Game game;

        //开始音乐
        SoundPlayer beginPlayer = new SoundPlayer(Properties.Resources.begin);

        public FrmMain()
        {
            //播放开始音乐
            beginPlayer.Play();
            InitializeComponent();
        }
        //处理键盘
        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (game != null)
            {
                //按键传给game
                game.KeyDown(e.KeyCode.ToString());
                GameArea.Invalidate();
            }

        }

        private void GameArea_Load(object sender, EventArgs e)
        {
            //初始化游戏类
            game = new Game();
            game.gameChanged += Game_gameChanged;
           
        }

        private void Game_gameChanged()
        {            
            if (game != null)
            {
                GameArea.Invalidate();
            }
            //游戏结束
            if (game.GameOver() == true)
            {                
                //停时间
                game.Timer.Stop();
                //弹盒子
                MessageBox.Show("游戏失败凸(⊙▂⊙✖ )");
                //初始化
                game = new Game();
                //重绘
                this.GameArea.Invalidate();
            }
            if (game.Victory() == true)
            {
                //停时间
                game.Timer.Stop();
                //弹盒子
                MessageBox.Show("游戏胜利O(∩_∩)O");
                //初始化
                game = new Game();
                game.gameChanged += Game_gameChanged;
                //重绘
                this.GameArea.Invalidate();
            }
        }

        private void GameArea_Paint(object sender, PaintEventArgs e)
        {
            //在游戏区域里调用Game的Draw函数，绘制背景
            if (game != null)
            {
                game.Draw(e.Graphics, this.GameArea.Size);
            }
        }

        //“开始游戏”按键
        private void btnStart_Click(object sender, EventArgs e)
        {
            //停止开始音乐
            beginPlayer.Stop();
            //播放背景音乐
            this.backgroundWorker1.RunWorkerAsync();
            //开始界面隐藏
            this.pictureBox1.Hide();
            //按键隐藏
            this.btnStart.Hide();
            this.btnIntroduction.Hide();
            //初始化游戏
            game = new Game();
            //游戏改变
            game.gameChanged += Game_gameChanged;
            //重绘
            this.GameArea.Invalidate();

        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

        }
        //“关于我”按键
        private void btnAboutMe_Click(object sender, EventArgs e)
        {
            //弹出介绍框
            FrmAboutMe frm = new FrmAboutMe();
            frm.ShowDialog();
        }

        private void btnStartGame_Paint(object sender, PaintEventArgs e)
        {

        }

        //游戏暂停
        private void btnPause_Click(object sender, EventArgs e)
        {
            game.Timer.Enabled = false;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        //绘制开始界面
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(Properties.Resources.face, new Rectangle(0,0,450,335));
        }

        //游戏重新开始
        private void btnStart2_Click(object sender, EventArgs e)
        {
            //初始化游戏
            game = new Game();
            //游戏改变
            game.gameChanged += Game_gameChanged;
            //重绘
            this.GameArea.Invalidate();

        }

        //绘制介绍界面
        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(Properties.Resources.introduction, new Rectangle(0, 0, 450, 335));
        }

        //弹出介绍面板
        private void btnIntroduction_Click(object sender, EventArgs e)
        {
            pictureBox2.BringToFront();
            btnok.BringToFront();
        }

        //隐藏介绍面板
        private void btnok_Click(object sender, EventArgs e)
        {
            pictureBox2.SendToBack();
            btnok.SendToBack();
        }

        //游戏继续
        private void btnGoOn_Click(object sender, EventArgs e)
        {
            game.Timer.Enabled = true;
        }

        //游戏背景音乐
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //SoundPlayer soundPlayer = new SoundPlayer(Properties.Resources.Start);
            //soundPlayer.LoadAsync();
            //soundPlayer.PlaySync();
            //soundPlayer.PlayLooping();            
        }
    }

}
